package cn.jolyoulu.common.mybatis.plus.config;

import cn.jolyoulu.common.mybatis.plus.handler.DefaultMetaObjectHandler;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import cn.jolyoulu.common.mybatis.plus.plugin.page.PageQueryPlugin;

import javax.sql.DataSource;
import java.util.Objects;

/**
 * @Author: JolyouLu
 * @Date: 2023/1/20 15:11
 * @Description Mybatis分页插件配置
 */
@Configuration
@EnableConfigurationProperties(value = {MybatisPlusProperties.class})
public class MybatisPlusConfig {

    @Bean
    public SqlSessionFactory MybatisSqlSessionFactory(DataSource dataSource,
                                                      MetaObjectHandler metaObjectHandler,
                                                      MybatisPlusProperties mybatisPlusProperties) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(mybatisPlusProperties.getXmlPath())
        );
        factoryBean.setDataSource(dataSource);
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        //判断是否有添加逻辑删除的配置
        MybatisPlusProperties.LogicDelete logicDelete = mybatisPlusProperties.getLogicDelete();
        if (Objects.nonNull(logicDelete)){
            dbConfig.setLogicDeleteField(logicDelete.getLogicDeleteField())
                    .setLogicNotDeleteValue(logicDelete.getLogicNotDeleteValue())
                    .setLogicDeleteValue(logicDelete.getLogicDeleteValue());
        }
        globalConfig.setDbConfig(dbConfig.setIdType(IdType.ASSIGN_UUID));
        globalConfig.setMetaObjectHandler(metaObjectHandler);
        factoryBean.setGlobalConfig(globalConfig);
        //添加插件
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(new PageQueryPlugin());
        factoryBean.setConfiguration(configuration);
        return factoryBean.getObject();
    }

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    @ConditionalOnMissingBean(MetaObjectHandler.class)
    public DefaultMetaObjectHandler defaultMetaObjectHandler(){
        return new DefaultMetaObjectHandler();
    }
}
